import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";
// import List from "../views/List.vue";
// import My from "../views/My.vue";
// import Cart from "../views/Cart.vue";
// import Search from '../views/Search.vue'

const originalPush = VueRouter.prototype.push
const originalReplace = VueRouter.prototype.replace
// push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
// replace
VueRouter.prototype.replace = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
  return originalReplace.call(this, location).catch(err => err)
}

Vue.use(VueRouter);

const routes = [{
	path: "/home",
	name: "Home",
	component: Home,
},
{
	path: '/',
	redirect: '/home'
},
{
	path: "/list",
	name: 'List',
	component: () => import("../views/List.vue")
},
{
	path: "/cart",
	name: 'Cart',
	component: () => import("../views/Cart.vue")
},
{
	path: "/my",
	name: 'My',
	component: () => import("../views/My.vue")
},
{
	path: '/search',
	children: [
		{
			path: '/',
			name: 'index',
			component: () => import("../views/search/SearchIndex.vue")
		},
		{
			path: 'list',
			name: 'SearchList',
			component: () => import("../views/search/SearchList.vue")
		}
	],
	component: () => import("../views/search/Search.vue")
},
{
	path: "/detail",
	name: 'Detail',
	component: () => import("../views/Detail.vue"),
	meta: {
		keepAlive: true,  //代表需要缓存
	},
},
{
	path: "/login",
	name: 'Login',
	component: () => import("../views/Login.vue")
},
{
	path: "/userLogin",
	name: 'UserLogin',
	component: () => import("../views/UserLogin.vue")
},
{
	path: "/register",
	name: 'Register',
	component: () => import("../views/Register.vue")
},
{
	path: "/recovery",
	name: 'Recovery',
	component: () => import("../views/recovery/Recovery.vue"),
	children: [
		{
			path: '/',
			name: 'RecoveryIndex',
			component: () => import("../views/recovery/RecoveryIndex.vue")
		},
		{
			path: 'btn',
			name: 'Btn',
			component: () => import("../views/recovery/RecoveryBtn.vue")
		}
	]
},
{
	path: "/path",
	component: () => import("../views/Path/Path.vue"),
	children: [
		{
			path: '/',
			name: 'PathList',
			component: () => import("../views/Path/PathList.vue")
		},
		{
			path: 'edit',
			name: 'Edit',
			component: () => import("../views/Path/Edit.vue")
		},
		{
			path: 'editInfo',
			name: 'EditInfo',
			component: () => import("../views/Path/EditInfo.vue")
		}
	]
},
{
	path: "/order",
	name: 'Order',
	component: () => import("../views/Order.vue"),
	meta: {
		keepAlive: true,  //代表需要缓存
	},
},
{
	path: "/payment",
	name: 'Payment',
	component: () => import("../views/Payment.vue")
},
];

const router = new VueRouter({
	mode: "history",
	base: process.env.BASE_URL,
	routes,
});
// 路由导航守卫
router.beforeEach((to,form,next)=>{
	let nextName = ['Cart','editInfo','edit','Order','Payment','PathList']
	let userInfo = JSON.parse(localStorage.getItem('teaUserInfo'))
	if(nextName.indexOf(to.name)>=0){
		if(!userInfo){
			router.push('/login')
		}
	}
	next()
})
export default router;
